#include"oled.h"

unsigned char FontBuf[128];

void send_command_to_ZK(unsigned char dat)
{
	unsigned char i;

	for (i=0; i<8; i++) {
		if (dat & 0x80)
			SDIN_SET;
		else
			SDIN_CLR;
		dat = dat << 1;
		SCLK_CLR;
		SCLK_SET;
	}
}

//-------------------------------------------------
unsigned char ZK_read(void)
{
	unsigned char cnt_clk;
	unsigned char ret_data=0;

	for (cnt_clk = 0; cnt_clk < 8; cnt_clk++) {
		//SDO_ZK_set;
		SCLK_CLR;  //字库时钟拉低
		ret_data = ret_data << 1;
		if (SDO_READ) {
			ret_data = ret_data + 1;
		} else {
			ret_data = ret_data + 0;
		}
		SCLK_SET; //字库时钟拉高
	}

	return  ret_data;
}

//=================================================================
//从DataAddress这个地址连续读出DataLen个字节的数据到 pBuff的地址
//连续读取
//=================================================================
void ZK_Read_1_n(unsigned char addrHigh, unsigned char addrMid, unsigned char addrLow, unsigned char *pBuff, unsigned char DataLen)
{
	unsigned char i;

 	CS_ZK_CLR;
	CS_SET;
	SCLK_CLR;
	send_command_to_ZK(0x03);
	send_command_to_ZK(addrHigh);
	send_command_to_ZK(addrMid);
	send_command_to_ZK(addrLow);
	for (i=0; i<DataLen; i++) {
		*(pBuff + i) = ZK_read();
	}
	CS_ZK_SET;
}

//***************************************************************
//  显示 ASCII 2015-11晶奥测试通过
//zk_num定义：汉字库的标示符 1:5x7 ASCII,2:7x8 ASCII, 3:6x12 ASCII, 4:8x16 ASCII,	5: 12x24 ASCII,6:16x32 ASCII;
//   x: Start Column  开始列
//   y: Start Row   开始行 0~63
//***************************************************************
void Display_Asc(unsigned char zk_num, unsigned int x, unsigned int y)
{
    unsigned char j, x0 = 1, y0 = 7, n = 8, k = 0;
    unsigned int x1 = x / 4;

	switch (zk_num) {
	    case '1':  x0=1;y0=7; n=8; k=0; break;   //	  5x7 ASCII
	    case '2':  x0=1;y0=7; n=8; k=1; break;   //	  7x8 ASCII
	    case '3':  x0=1;y0=11; n=12; k=1;  break;   //  6x12 ASCII
	    case '4':  x0=1;y0=15; n=16; k=1;  break;   //  8x16 ASCII
	    case '5':  x0=3;y0=23; n=48; k=1;  break;   //  12x24 ASCII
	    case '6':  x0=3;y0=31; n=64; k=1;  break;   //  16x32 ASCII
	}

	Set_Column_Address(Shift + x1, Shift + x1 + x0); // 设置列坐标，shift为列偏移量由1322决定。
	Set_Row_Address(y, y + y0); //设置行坐标
	Set_Write_RAM();	 //	写显存

	if (k == 0) {
		for (j=0; j<n-1; j++) {
			con_4_byte(FontBuf[j]);//数据转换，把数组数据送入SSD1322
		}
		Write_Data(0x00);	    //写前2列
		Write_Data(0x00);             //写后2列	  共计4列
	} else {
	 	for (j=0; j<n; j++) {
			con_4_byte(FontBuf[j]);//数据转换，把数组数据送入SSD1322
		}
	}
}

//*************************************************************************
//****	Display_Asc_String  ASCII字符串显示程序	***//
//zk_num定义：汉字库的标示符 1:5x7 ASCII,2:7x8 ASCII, 3:6x12 ASCII, 4:8x16 ASCII,	5: 12x24 ASCII,6:16x32 ASCII;
//x：列坐标 y：行坐标
//text[]：要显示的ASCII
//**************************************************************************
void Display_Asc_String(unsigned char zk_num,unsigned int x, unsigned int y, unsigned char  text[])
{
	unsigned char i = 0;
	unsigned char AddrHigh, AddrMid, AddrLow ; //字高、中、低地址
	unsigned long FontAddr = 0; //字地址
	unsigned long BaseAdd = 0; //字库基地址
    unsigned char n = 0, d = 0, c = 0;// 不同点阵字库的计算变量

	switch (zk_num) {
		case '1':  BaseAdd=0x1DDF80; n=8;d=8;c=8;   break;	 //	  5x7 ASCII
		case '2':  BaseAdd=0x1DE280; n=8;d=8;c=8;   break;	 //   7x8 ASCII
	    case '3':  BaseAdd=0x1DBE00; n=12;d=8;c=12; break;	 //  6x12 ASCII
	    case '4':  BaseAdd=0x1DD780; n=16;d=8;c=16; break;	 //  8x16 ASCII
		case '5':  BaseAdd=0x1DFF00; n=48;d=12;c=24;break;	 //  12x24 ASCII
		case '6':  BaseAdd=0x1E5A50; n=64;d=16;c=32;break;	 //  16x32 ASCII
	}

	while ((text[i] > 0x00)) {
		if (x > (256 - d)) {
			y = y + c;
			x=0;
		}//溢出换行

	   	if ((text[i] >= 0x20) &&(text[i] <= 0x7E)) {
			FontAddr = text[i] - 0x20;
			FontAddr = (unsigned long)((FontAddr * n) + BaseAdd);
			AddrHigh = (FontAddr & 0xff0000) >> 16;  /*地址的高8位,共24位*/
			AddrMid = (FontAddr & 0xff00) >> 8;      /*地址的中8位,共24位*/
			AddrLow = FontAddr & 0xff;	     /*地址的低8位,共24位*/
			ZK_Read_1_n(AddrHigh, AddrMid, AddrLow, FontBuf, n);/*取一个汉字的数据，存到"FontBuf[]"*/
			Display_Asc(zk_num, x, y);/*显示一个ascii到OLED上 */
			i += 1;
			x += d;//下一个字坐标
		} else {
			break;
		}
	}
}
